package com.ego.passport.service.impl;

import com.ego.commons.constant.Constant;
import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.CartPojo;
import com.ego.commons.pojo.EgoResult;
import com.ego.commons.pojo.ShopCart;
import com.ego.commons.utils.CookieUtils;
import com.ego.commons.utils.ServletUtil;
import com.ego.dubbo.service.PassportDubboService;
import com.ego.passport.service.PassportService;
import com.ego.pojo.TbUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

@Service
public class PassportServiceImpl implements PassportService {

    @Autowired
    private RedisTemplate redisTemplate;

    @DubboReference
    private PassportDubboService passportDubboService;

    @Override
    public EgoResult checkUser(String param, int type) {

        TbUser tbUser = new TbUser();

        if(type==1){
            tbUser.setUsername(param);
        }else if(type ==2){
            tbUser.setPhone(param);
        }else if(type ==3){
            tbUser.setEmail(param);
        }

        TbUser selectUser = passportDubboService.selectByTbUser(tbUser);

        //如果查询的TbUser对象为空，说明数据库中没有相关数据，可以注册

        return selectUser==null?EgoResult.ok():EgoResult.error("");
    }

    @Override
    public EgoResult register(TbUser tbUser) {

        Date now = new Date();
        tbUser.setCreated(now);
        tbUser.setUpdated(now);

        //使用Md5加密密码
        tbUser.setPassword(DigestUtils.md5DigestAsHex(tbUser.getPassword().getBytes()));

        try {
            int row = passportDubboService.register(tbUser);

            if(row == 1){
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }


        return EgoResult.error("注册用户失败");


    }

    @Override
    public TbUser login(TbUser tbUser) {

        tbUser.setPassword(DigestUtils.md5DigestAsHex(tbUser.getPassword().getBytes()));
        TbUser loginUser = passportDubboService.login(tbUser);
        //合并购物车 ：将临时购物车和用户购物车数据合并
        /**
         * 思路
         * 1，获取临时购物车数据
         * 2，获取用户购物车
         * 3，如果临时购物车数据集合大于0说明临时购物车才有数据 ，才有必要同步，否则不同步
         */
        if(loginUser!=null){

            //获取临时购物车
            ShopCart tempShopCart = ShopCart.getShopCartByCookie();
            if(tempShopCart==null){
                tempShopCart = new ShopCart();
            }
            List<CartPojo> tempCartList = tempShopCart.getCartList(); //0-N 如果临时购物车没有任何数据没有必要同步


            //获取用户购物车
            ShopCart userShopCart = (ShopCart) redisTemplate.opsForValue().get(Constant.LOGIN_USER_REDIS_SHOP_CART_KEY_PREFIX+loginUser.getUsername());
            if(userShopCart==null){
                userShopCart = new ShopCart();
            }

            //如果临时购物车没有任何数据没有必要同步,有数据才同步
            if(tempCartList.size()>0){
                for (CartPojo cartPojo : tempCartList) {
                    //将临时购物车的数据添加到用户购物车
                    userShopCart.addToCart(cartPojo);

                }
            }
            //存储用户购物车
            redisTemplate.opsForValue().set(Constant.LOGIN_USER_REDIS_SHOP_CART_KEY_PREFIX+loginUser.getUsername(),userShopCart);

            //清空临时购物车
            CookieUtils.deleteCookie(ServletUtil.getRequest(),ServletUtil.getResponse(),Constant.CART_NAME);

        }

        return  loginUser;

    }


    public static void main(String[] args) {

        String password = DigestUtils.md5DigestAsHex("123456".getBytes());


        System.out.println("password = " + password);//e10adc3949ba59abbe56e057f20f883e


    }

}
